﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STRobot
{
    public partial class MapNameAddWnd : Form
    {
        public MapNameAddWnd(int index = -1)
        {
            InitializeComponent();
            index_ = index;
            if (index_ > -1)
            {
                button1.Text = "保存";
                if (Config.Get().jumppoints[index_].starmapname == null)
                {
                    textBox1.Text = "地图异常null";
                }
                else
                {
                    textBox1.Text = Config.Get().jumppoints[index_].starmapname;
                }
               
                jumppoint_ = Config.Get().jumppoints[index_];
            }
            else
            {
                jumppoint_ = new Jumppoint();
            }
            jumpointlistshow();
        }
        int index_;
        Jumppoint  jumppoint_;
        //所有跳转点显示
        private void jumpointlistshow()
        {
            if (jumppoint_.targetmap == null)
            {
                return;
            }
            listBox1.Items.Clear();
            foreach (var item in jumppoint_.targetmap)
            {
                listBox1.Items.Add(item.showname + ":" + item.targetname);
            }
        }

        //保存
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length == 0)
            {
                MessageBox.Show("需要填写");
                return;
            }
            jumppoint_.starmapname = textBox1.Text;
            if (index_ > -1)
            {
                Config.Get().jumppoints[index_] = jumppoint_;
            }
            else
            {
                Config.Get().jumppoints.Add(jumppoint_);
            }
            DialogResult = DialogResult.OK;
            Close();
        }

        //添加跳转点
        private void button2_Click(object sender, EventArgs e)
        {
            var wnd = new JumpPointAddWnd();
            wnd.Text = "跳转点设置";
            if (wnd.ShowDialog() == DialogResult.OK)
            {
                var jump = wnd.GetJupTargetPoint();
                if (jump.targetname != null && jump.showname != null && jump.showname.Length > 0 && jump.targetname.Length > 0)
                {
                    if (jumppoint_.targetmap == null)
                    {
                        jumppoint_.targetmap = new List<JumpTargetpoint>();
                    }
                    jumppoint_.targetmap.Add(jump);
                }
                jumpointlistshow();
            }

        }
        //当前地图跳转点编辑
        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listBox1.SelectedIndex == -1) return;
            var wnd = new JumpPointAddWnd(jumppoint_.targetmap[listBox1.SelectedIndex]);
            if (wnd.ShowDialog() == DialogResult.OK)
            {
                var jump = wnd.GetJupTargetPoint();
                if (jump.showname.Length > 0 && jump.targetname.Length > 0)
                {
                    jumppoint_.targetmap[listBox1.SelectedIndex] = jump;
                    jumpointlistshow();
                }
                
            } 

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            {
                var str = "确认删除 [" + jumppoint_.targetmap[listBox1.SelectedIndex].showname + "] 吗?";
                if (MessageBox.Show(str, "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    jumppoint_.targetmap.RemoveAt(listBox1.SelectedIndex);
                    jumpointlistshow();
                }
            }
        }
    }
}
